using System;
using System.Collections.Generic;
using System.Text;

namespace Adoor.Collections.Generic
{
    [Serializable] 
    public class List<T> : IList<T>
    {
        private System.Collections.Generic.List<T> innerList;
        public event CollectionChangeEventHandler<T> CollectionChanged;

        protected virtual void OnCollectionChanged(CollectionChangeAction action, T element)
        {
            if (CollectionChanged != null)
            {
                CollectionChangeEventArgs<T> e = new CollectionChangeEventArgs<T>(action, element);
                CollectionChanged(this, e);
            }
        }

        public List()
        {
            innerList = new System.Collections.Generic.List<T>();
        }

        #region IList<T> Members

        public virtual int IndexOf(T item)
        {
            return innerList.IndexOf(item);
        }

        public virtual void Insert(int index, T item)
        {
            innerList.Insert(index, item);
            OnCollectionChanged(CollectionChangeAction.Add, item);
        }

        public virtual void RemoveAt(int index)
        {
            T item = innerList[index];
            innerList.RemoveAt(index);
            OnCollectionChanged(CollectionChangeAction.Remove, item);
        }

        public virtual T this[int index]
        {
            get
            {
                return innerList[index];
            }
            set
            {
                innerList[index] = value;
                OnCollectionChanged(CollectionChangeAction.Add, value);
            }
        }

        #endregion

        #region ICollection<T> Members

        public virtual void Add(T item)
        {
            innerList.Add(item);
            OnCollectionChanged(CollectionChangeAction.Add, item);
        }

        public virtual void Clear()
        {
            innerList.Clear();
            OnCollectionChanged(CollectionChangeAction.Refresh, default(T));
        }

        public virtual bool Contains(T item)
        {
            return innerList.Contains(item);
        }

        public virtual void CopyTo(T[] array, int arrayIndex)
        {
            innerList.CopyTo(array, arrayIndex);
        }

        public virtual int Count
        {
            get { return innerList.Count; }
        }

        public virtual bool IsReadOnly
        {
            get { return false; }
        }

        public virtual bool Remove(T item)
        {
            bool result = innerList.Remove(item);
            if (result)
                OnCollectionChanged(CollectionChangeAction.Remove, item);
            return result;
        }

        #endregion

        #region IEnumerable<T> Members

        public virtual IEnumerator<T> GetEnumerator()
        {
            return innerList.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((System.Collections.IEnumerable)innerList).GetEnumerator();
        }

        #endregion
    }
}
